package com.qianf.qfoffice.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qianf.qfoffice.entity.Depart;
import com.qianf.qfoffice.entity.DepartNum;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.service.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
public class DepartController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @Autowired
    private DepartService departService;

    /**
     * 折线图
     * @return
     */
    @RequestMapping("/getDepartNum.do")
    public ResponseData<DepartNum> getDepartNum(){

        ResponseData<DepartNum> responseData = new ResponseData<>();

        try{
            DepartNum departNum = departService.getDepartNum();

            responseData.setCode(0);
            responseData.setData(departNum);
        }catch (Exception e){
            responseData.setCode(1);
            responseData.setMsg("服务异常");
        }

        return responseData;

    }

    /**
     * 查询所有部门信息
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/departpage.do")
    public ResponseData selectAllDepart(int page, int limit){
        // 开启分页
        PageHelper.startPage(page, limit);

        List<Depart> departList = departService.findAllDepart(page, limit);
        // 得到分页相关信息
        PageInfo pageInfo = new PageInfo(departList);

        ResponseData<Object> responseData = new ResponseData<>();

        responseData.setCode(0);
        responseData.setCount((int) pageInfo.getTotal());
        responseData.setData(departList);

        return responseData;
    }

    /**
     * 增加部门
     * @param depart
     * @return
     */
    @RequestMapping("/addDepart.do")
    public ResponseData addDepart(Depart depart){

        System.out.println("depart = " + depart);
        ResponseData<Object> responseData = new ResponseData<>();

        depart.setFlag(1);

        int i = departService.insertDepart(depart);

        if (i > 0){
            responseData.setCode(1);
            responseData.setMsg("添加成功");
        }else {
            responseData.setCode(0);
            responseData.setMsg("添加失败");
        }

        return responseData;
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping("/departdelete.do")
    public ResponseData departDelete(int id){

        ResponseData<Object> responseData = new ResponseData<>();

        int i = departService.deleteDepart(id);
        if (i > 0){
            responseData.setCode(1);
            responseData.setMsg("删除成功");
            return responseData;
        }else {
            responseData.setCode(0);
            responseData.setMsg("删除失败");
        }
        return responseData;
    }

    @RequestMapping("/updateDepart.do")
    public ResponseData updateDepartById(Depart depart){

        ResponseData<Object> responseData = new ResponseData<>();

        int i = departService.updateDepart(depart);
        if (i > 0){
            responseData.setCode(1);
            responseData.setMsg("更新成功");
            return responseData;

        }else {
            responseData.setCode(0);
            responseData.setMsg("更新失败");
        }
        return responseData;

    }


}
